function varargout = TLScheduleDlg(action, varargin)

% TL_SCHEDULE_DLG manages the user interface of TargetLink Schedule block.
%
% SYNTAX dlgInfo = TLScheduleDlg('GetDlgInfo');
%   gets dialog info of TargetLink Schedule block
%
%   INPUT ARGUMENTS
%   -/-      
%
%   OUTPUT ARGUMENTS
%   dlgInfo     dialog information contains pageNames etc.
%
% SYNTAX [sheetDims, ctrlPos] = TLScheduleDlg('CtrlPositions', pageName, commonGeom, dlgInfo, sheetDims);
%   calculates the control positions of all controls of dialog page: <pageName>
%
%   INPUT ARGUMENTS
%   pageName        name of current page
%   commonGeom      common geometry data
%   dlgInfo         dialog information contains pageNames etc.
%   sheetDims       dimension of sheet
%
%   OUTPUT ARGUMENTS
%   sheetDims       modified dimension of sheet
%   ctrlPos         positions of controls
%
% SYNTAX dlgdata = TLScheduleDlg('Create', pageName, dlgfig, dlgdata);
%   creates all controls of dialog page: <pageName>
%
%   INPUT ARGUMENTS
%   pageName        name of current page
%   dlgfig          figure handle
%   dlgdata         dialog data
%
%   OUTPUT ARGUMENTS
%   dlgdata         modified dialog data
%
% SYNTAX [dlgdata,bModified] = TLScheduleDlg('Manage', pageName, dlgfig, dlgdata, pageAction);
%   manages all actions of dialog page: <pageName>
%
%   INPUT ARGUMENTS
%   pageName        name of current page
%   dlgfig          figure handle
%   dlgdata         dialog data
%   pageAction      modified control of current page
%
%   OUTPUT ARGUMENTS
%   dlgdata         modified dialog data
%   bModified       flag = 1, if dialog data are modified, =0 otherwise
%
% SYNTAX dlgdata = TLScheduleDlg('Update', dlgfig, dlgdata, pageNum);
%   updates all controls of dialog page: <pageNum>
%
%   INPUT ARGUMENTS
%   dlgfig          figure handle
%   dlgdata         dialog data
%   pageNum         number of current page
%
%   OUTPUT ARGUMENTS
%   dlgdata         modified dialog data
%
% SYNTAX TLScheduleDlg('Apply', dlgfig, dlgdata);
%   writes current blockdata to block and DD
%
%   INPUT ARGUMENTS
%   dlgfig      handle for dialog figure
%   dlgdata     dialog data
%
%   OUTPUT ARGUMENTS
%   - / -


% Author(s): D. Andoleit, T. Pietzsch
% $RCSfile: TLScheduleDlg.m $
% $ProjectName: e:/ARC/Components/HostSW/SimulinkBlocksets/TLBlocksets/MultirateBlockset/MultirateBlockset_UI/Sources/MSrc/project.pj $
% $Revision: 1.6 $
% $Date: 2011/03/18 07:25:06MEZ $
%
% Copyright ?2011 dSPACE GmbH. All rights reserved.


switch action
    case 'GetDlgInfo'
        hBlock = varargin{1};
        dlgInfo = tl_get_dlginfodefaults('rtos');
        titleName = 'Schedule';
        pathinfo = tl_pathinfo(hBlock);
        dlgInfo.title = sprintf('TargetLink %s: %s', titleName, pathinfo.shortDisplayName);
        dlgInfo.pageNames   = {
            'Schedule'};
        varargout{1} = dlgInfo;
        varargout{2} = get_tlcg_data(hBlock);

    case 'CtrlPositions'
        pageName     = varargin{1};
        commonGeom   = varargin{2};
        sheetDims    = varargin{4};
        switch pageName
            case 'Schedule'
                [sheetDims, ctrlPos] = FcnCalcScheduleControlPositions(commonGeom);
        end
        varargout{1} = sheetDims;
        varargout{2} = ctrlPos;

    case 'Create',
        pageName = varargin{1};
        dlgfig   = varargin{2};
        dlgdata  = varargin{3};
        pageNum  = strmatch(pageName, dlgdata.pageNames, 'exact');
        switch pageName
            case 'Schedule'
                dlgdata = FcnCreateScheduleControls(pageNum,dlgfig,dlgdata);
        end
        dlgdata.pageData{pageNum}.ctrlPos = [];
        varargout{1} = dlgdata;

    case 'Manage',
        % do nothing

    case 'Update'
        % do nothing
        varargout{1} = varargin{2};

    case 'Apply'
        dlgdata = varargin{2};
        bClose   = varargin{3};
        [dlgdata, bClose] = tl_apply_blockdata(dlgdata, bClose);
        varargout{1} = dlgdata;
        varargout{2} = bClose;

    otherwise
        error(['Invalid action: "' action '"']);
end


%-----------------------------------------------------------------------------------------
% FcnCalcScheduleControlPositions calculates position for Schedule description
%
% INPUT ARGUMENTS
%   commonGeom		common geometry values
%
% OUTPUT ARGUMENTS
%   sheetDims		dimension of sheet
%   ctrlPos		calculated positios of al controls
%
% HINT:
% Positions are calculated from the bottom left of the fig to avoid
% dependencies on figure size
%-----------------------------------------------------------------------------------------
function [sheetDims, ctrlPos] = FcnCalcScheduleControlPositions(commonGeom)

strings_and_styles = {...
    repmat('m',1,40),           'text'};

[descTextLineSize] = ...
    ds_get_uicontrol_sizes(commonGeom, strings_and_styles);

descTextSize(1) = descTextLineSize(1);
descTextSize(2) = 3 * descTextLineSize(2);

cx = commonGeom.sheetSideEdgeBuffer + ...
    commonGeom.figSideEdgeBuffer;

cy = commonGeom.bottomSheetOffset + ...
    commonGeom.sheetBottomEdgeBuffer;

scheduleGroupboxWidth = ...
    commonGeom.frameSideEdgeBuffer + ...
    descTextSize(1) + ...
    commonGeom.frameSideEdgeBuffer;

scheduleGroupboxHeight = ...
    commonGeom.frameTopEdgeBuffer + ...
    3 * commonGeom.editHeight + ...
    commonGeom.frameBottomEdgeBuffer;

ctrlPos.scheduleGroupbox =  ...
    [cx cy  scheduleGroupboxWidth scheduleGroupboxHeight];

%-----------------------------------------------------------
% text
cy = commonGeom.frameBottomEdgeBuffer;

% schedule description
cx = commonGeom.frameSideEdgeBuffer;
ctrlPos.descText = [cx cy descTextSize];

% sheet dimensions
sheetDims(1) = ctrlPos.scheduleGroupbox(3) + ...
    2 * commonGeom.frameSideEdgeBuffer;

sheetDims(2) = ctrlPos.scheduleGroupbox(4) + ...
    commonGeom.textHeight + ...
    commonGeom.sheetBottomEdgeBuffer;


%-----------------------------------------------------------------------------------------
% FcnCreateScheduleControls creates schedule description controls
%
% INPUT ARGUMENTS
%   pageNum 	number of current page (=1 for schedule page)
%   dlgfig      dialog figure handle
%   dlgdata 	dialog data
%
% OUTPUT ARGUMENTS
%   dlgdata 	modified diaog data
%-----------------------------------------------------------------------------------------
function dlgdata = FcnCreateScheduleControls(pageNum,dlgfig,dlgdata)

ctrlPos    = dlgdata.pageData{pageNum}.ctrlPos;
Children   = dlgdata.pageData{pageNum}.children;

% ISR groupbox
Children.scheduleGroupbox = uipanel( ...
    'Parent',            dlgfig, ...
    'Title',            {'Description'}, ...
    'Units',            'pixels', ...
    'Position',          ctrlPos.scheduleGroupbox);

descText = {[ ...
    'This block can be placed in an atomic subsystem. This results ', ...
    'in a call of the OSEK API function Schedule() at the end of ', ...
    'the computation algorithm representing the atomic subsystem.'] ...
    };

% description
Children.descText = uicontrol( ...
    'Parent',             Children.scheduleGroupbox, ...
    'Style',              'text', ...
    'String',             descText, ...
    'Position',           ctrlPos.descText);

dlgdata.pageData{pageNum}.children = Children;
